"""
    Author:
    Date: 2019-08-17

    微信管理后台-通话管理
"""

from django.shortcuts import render


from .. import helpers
from .. helpers import gma_login_required
from entities import animoji_mgr
from entities import ban_words_mgr
from entities import banner_mgr
from entities import gift_mgr
from entities import room_mgr
from entities import reply_mgr
from entities.consts import *

from utils import ajax
from utils.log import Log


@gma_login_required
def ban_words(request):
    """ 屏蔽词列表 """
    context = helpers.build_context(request)
    context['data'] = ban_words_mgr.get_ban_words()
    return render(request, 'gma/config/config_ban_words.html', context)


# -----------------------------------------------------------------------------
# Banner
# -----------------------------------------------------------------------------

@gma_login_required
def banner_home(request):
    """ banner 配置首页 """
    context = helpers.build_context(request)
    context['homepage_banners'] = banner_mgr.get_banner_list(BANNER_OF_HOME)
    context['rooms_banners'] = banner_mgr.get_banner_list(BANNER_OF_ROOMS)
    context['custom_decoration_banners'] = banner_mgr.get_banner_list(BANNER_OF_CUSTOM_DECORATION)
    return render(request, 'gma/config/banner/config_banner_home.html', context)


@gma_login_required
def new_banner(request, banner_type):
    """ 添加banner """
    context = helpers.build_context(request)
    context['banner_type'] = banner_type
    return render(request, 'gma/config/banner/config_new_banner.html', context)


@gma_login_required
def edit_banner(request, banner_id):
    """ 编辑banner """
    banner = banner_mgr.get_banner(banner_id)
    context = helpers.build_context(request)
    context['banner'] = banner
    return render(request, 'gma/config/banner/config_edit_banner.html', context)


@gma_login_required
def homepage_banners(request):
    """ 首页banner配置 """
    context = helpers.build_context(request)
    context['banners'] = banner_mgr.get_banner_list(BANNER_OF_HOME)
    context['banner_type'] = BANNER_OF_HOME
    return render(request, 'gma/config/banner/config_homepage_banners.html', context)


@gma_login_required
def rooms_banners(request):
    """ 聊天室banner配置 """
    context = helpers.build_context(request)
    context['banners'] = banner_mgr.get_banner_list(BANNER_OF_ROOMS)
    context['banner_type'] = BANNER_OF_ROOMS
    return render(request, 'gma/config/banner/config_rooms_banners.html', context)


@gma_login_required
def custom_decoration_banners(request):
    """ 个性定制页面banner配置 """
    context = helpers.build_context(request)
    context['banners'] = banner_mgr.get_banner_list(BANNER_OF_CUSTOM_DECORATION)
    context['banner_type'] = BANNER_OF_CUSTOM_DECORATION
    return render(request, 'gma/config/banner/config_custom_decoration_banners.html', context)

# -----------------------------------------------------------------------------
# Gift
# -----------------------------------------------------------------------------

@gma_login_required
def gift_home(request):
    """ 礼物配置首页 """
    context = helpers.build_context(request)
    context['gifts'] = gift_mgr.get_gift_list()
    return render(request, 'gma/config/gift/config_gift_home.html', context)


@gma_login_required
def gift_list(request, category_id = None):
    """ 礼物列表页面 """
    context = helpers.build_context(request)
    context['gifts'] = gift_mgr.get_gift_list(category_id)
    return render(request, 'gma/config/gift/config_gift_list.html', context)


@gma_login_required
def new_gift(request):
    """ 添加礼物 """
    context = helpers.build_context(request)
    return render(request, 'gma/config/gift/config_new_gift.html', context)


@gma_login_required
def edit_gift(request, gift_id):
    """ 编辑banner """
    context = helpers.build_context(request)
    context['gift'] = gift_mgr.get_gift(gift_id)
    return render(request, 'gma/config/gift/config_edit_gift.html', context)


@gma_login_required
def gift_category_list(request):
    """ 礼物分类列表页面 """
    context = helpers.build_context(request)
    # context['gifts'] = gift_mgr.get_gift_category_list()
    return render(request, 'gma/config/gift/config_gift_category_list.html', context)


@gma_login_required
def new_gift_category(request):
    """ 添加礼物分类 """
    context = helpers.build_context(request)
    context['new_category_id'] = gift_mgr.get_max_category_id() + 1
    return render(request, 'gma/config/gift/config_new_gift_category.html', context)


@gma_login_required
def edit_gift_category(request, category_id):
    """ 编辑banner """
    context = helpers.build_context(request)
    context['category'] = gift_mgr.get_gift_category(category_id)
    return render(request, 'gma/config/gift/config_edit_gift_category.html', context)


# -----------------------------------------------------------------------------
# Room
# -----------------------------------------------------------------------------

@gma_login_required
def room_home(request):
    """ 房间配置首页 """
    context = helpers.build_context(request)
    return render(request, 'gma/config/room/config_room_home.html', context)


@gma_login_required
def room_tag_list(request):
    """ 房间品类列表页面 """
    context = helpers.build_context(request)
    return render(request, 'gma/config/room/config_room_tag_list.html', context)


@gma_login_required
def api_get_room_tag_list(request):
    """ 请求房间品类列表 """
    tags = room_mgr.get_room_tag_list()
    rooms_count = room_mgr.get_room_count_group_by_tag()
    for tag in tags:
        tag['room_count'] = rooms_count.get(tag['tag_id'], 0)

    return ajax.success_response(tags)


@gma_login_required
def api_get_room_tag_list_in_homepage(request):
    """ 请求首页房间品类列表 """
    tags = room_mgr.get_room_tag_list_in_homepage()
    rooms_count = room_mgr.get_room_count_group_by_tag()
    for tag in tags:
        tag['room_count'] = rooms_count.get(tag['tag_id'], 0)

    return ajax.success_response(tags)


@gma_login_required
def api_get_room_tag_list_in_chat_room_page(request):
    """ 请求聊天室房间品类列表 """
    tags = room_mgr.get_room_tag_list_in_chat_room_page()
    rooms_count = room_mgr.get_room_count_group_by_tag()
    for tag in tags:
        tag['room_count'] = rooms_count.get(tag['tag_id'], 0)

    return ajax.success_response(tags)


@gma_login_required
def api_enable_room_tag_in_homepage(request):
    """ 启用首页的房间分类 """
    room_tag_id = int(request.POST['room_tag_id'].strip())
    ok = room_mgr.enable_room_tag_in_homepage(room_tag_id)
    if not ok:
        return ajax.error_response('启用失败!')
    return ajax.success_response(None, '启用成功!')


@gma_login_required
def api_disable_room_tag_in_homepage(request):
    """ 禁用首页的房间分类 """
    room_tag_id = int(request.POST['room_tag_id'].strip())
    ok = room_mgr.disable_room_tag_in_homepage(room_tag_id)
    if not ok:
        return ajax.error_response('禁用失败!')
    return ajax.success_response(None, '禁用成功!')


@gma_login_required
def api_enable_room_tag_in_chat_room_page(request):
    """ 启用首页的房间分类 """
    room_tag_id = int(request.POST['room_tag_id'].strip())
    ok = room_mgr.enable_room_tag_in_chat_room_page(room_tag_id)
    if not ok:
        return ajax.error_response('启用失败!')
    return ajax.success_response(None, '启用成功!')


@gma_login_required
def api_disable_room_tag_in_chat_room_page(request):
    """ 禁用首页的房间分类 """
    room_tag_id = int(request.POST['room_tag_id'].strip())
    ok = room_mgr.disable_room_tag_in_chat_room_page(room_tag_id)
    if not ok:
        return ajax.error_response('禁用失败!')
    return ajax.success_response(None, '禁用成功!')


@gma_login_required
def new_room_tag(request):
    """ 添加礼物分类 """
    context = helpers.build_context(request)
    context['new_tag_id'] = room_mgr.get_max_tag_id() + 1
    return render(request, 'gma/config/room/config_new_room_tag.html', context)


@gma_login_required
def edit_room_tag(request, tag_id):
    """ 编辑banner """
    context = helpers.build_context(request)
    context['tag'] = room_mgr.get_room_tag(tag_id)
    return render(request, 'gma/config/room/config_edit_room_tag.html', context)


@gma_login_required
def api_new_room_tag(request):
    """ 添加新的房间品类 """
    tag_id = int(request.POST['tag_id'].strip())
    tag_name = request.POST['tag_name'].strip()
    heat_factor = int(request.POST['heat_factor'].strip())
    orderno = int(request.POST['orderno'].strip())
    Log.info(f'Add new room tag: tag_id={tag_id}, name={tag_name}, heat={heat_factor}, orderno={orderno}')

    if not tag_name:
        return ajax.error_response('品类名称不可以为空!')
    if not heat_factor:
        return ajax.error_response('热度系数不可以为0!')

    ok = room_mgr.new_room_tag(tag_id, tag_name, heat_factor, orderno)
    if not ok:
        return ajax.error_response('添加失败!')

    return ajax.success_response()


@gma_login_required
def api_edit_room_tag(request):
    """ 编辑房间品类 """
    tag_id = int(request.POST['tag_id'].strip())
    tag_name = request.POST['tag_name'].strip()
    tag_heat_factor = int(request.POST['tag_heat_factor'].strip())
    tag_orderno = int(request.POST['tag_orderno'].strip())

    if not tag_id:
        return ajax.error_response('品类id不可以为空!')
    if not tag_name:
        return ajax.error_response('品类名称不可以为空!')
    if not tag_heat_factor:
        return ajax.error_response('热度系数不可以为0!')

    ok = room_mgr.edit_room_tag(tag_id, tag_name, tag_heat_factor, tag_orderno)
    if not ok:
        return ajax.error_response('修改失败!')
    return ajax.success_response(None, '修改成功!')


# -----------------------------------------------------------------------------
# Animoji
# -----------------------------------------------------------------------------

@gma_login_required
def animoji_home(request):
    """ 动态表情首页 """
    context = helpers.build_context(request)
    return render(request, 'gma/config/animoji/config_animoji_home.html', context)


@gma_login_required
def api_get_animoji_list(request):
    """ 请求房间品类列表 """
    animojis = animoji_mgr.get_animoji_list()
    return ajax.success_response(animojis)


@gma_login_required
def animoji_list(request):
    """ 动态表情列表页面 """
    context = helpers.build_context(request)
    return render(request, 'gma/config/animoji/config_animoji_list.html', context)


@gma_login_required
def api_enable_animoji(request):
    """ 启用动态表情 """
    animoji_id = int(request.POST['animoji_id'].strip())
    ok = animoji_mgr.enable_animoji(animoji_id)
    if not ok:
        return ajax.error_response('启用失败!')
    return ajax.success_response(None, '启用成功!')


@gma_login_required
def api_disable_animoji(request):
    """ 禁用动态表情 """
    animoji_id = int(request.POST['animoji_id'].strip())
    ok = animoji_mgr.disable_animoji(animoji_id)
    if not ok:
        return ajax.error_response('禁用失败!')
    return ajax.success_response(None, '禁用成功!')


@gma_login_required
def new_animoji(request):
    """ 添加动态表情 """
    context = helpers.build_context(request)
    context['new_animoji_id'] = animoji_mgr.get_max_animoji_id() + 1
    return render(request, 'gma/config/animoji/config_new_animoji.html', context)


@gma_login_required
def edit_animoji(request, animoji_id):
    """ 编辑动态表情 """
    context = helpers.build_context(request)
    context['animoji'] = animoji_mgr.get_animoji(animoji_id)
    return render(request, 'gma/config/animoji/config_edit_animoji.html', context)


@gma_login_required
def api_new_animoji(request):
    """ 添加新的动态表情 """
    id = request.POST['id'].strip()
    name = request.POST['name'].strip()
    resource = request.POST['resource'].strip()
    orderno = int(request.POST['orderno'].strip())
    Log.info(f'Add new animoji: name={name}, orderno={orderno}, resource={resource}')

    if not name:
        return ajax.error_response('名称不可以为空!')
    if not resource:
        return ajax.error_response('资源地址不可以为空!')

    ok = animoji_mgr.new_animoji(id, name, resource, orderno)
    if not ok:
        return ajax.error_response('添加失败!')

    return ajax.success_response()


@gma_login_required
def api_edit_animoji(request):
    """ 编辑动态表情 """
    animoji_id = int(request.POST['animoji_id'].strip())
    animoji_name = request.POST['animoji_name'].strip()
    animoji_resource = request.POST['animoji_resource'].strip()
    animoji_orderno = int(request.POST['animoji_orderno'].strip())

    if not animoji_name:
        return ajax.error_response('名称不可以为空!')
    if not animoji_resource:
        return ajax.error_response('资源地址不可以为空!')

    ok = animoji_mgr.edit_animoji(animoji_id, animoji_name, animoji_resource, animoji_orderno)
    if not ok:
        return ajax.error_response('修改失败!')
    return ajax.success_response(None, '修改成功!')


# -----------------------------------------------------------------------------
# Fast reply
# -----------------------------------------------------------------------------

@gma_login_required
def reply_home(request):
    """ 快速回复首页 """
    context = helpers.build_context(request)
    return render(request, 'gma/config/reply/config_reply_home.html', context)


@gma_login_required
def api_get_fast_reply_list(request):
    """ 请求快速回复列表 """
    replies = reply_mgr.get_fast_reply_list()
    return ajax.success_response(replies)


@gma_login_required
def fast_reply_list(request):
    """ 动态表情列表页面 """
    context = helpers.build_context(request)
    return render(request, 'gma/config/reply/config_fast_reply_list.html', context)


@gma_login_required
def api_enable_fast_reply(request):
    """ 启用动态表情 """
    fast_reply_id = int(request.POST['fast_reply_id'].strip())
    ok = reply_mgr.enable_fast_reply(fast_reply_id)
    if not ok:
        return ajax.error_response('启用失败!')
    return ajax.success_response(None, '启用成功!')


@gma_login_required
def api_disable_fast_reply(request):
    """ 禁用动态表情 """
    fast_reply_id = int(request.POST['fast_reply_id'].strip())
    ok = reply_mgr.disable_fast_reply(fast_reply_id)
    if not ok:
        return ajax.error_response('禁用失败!')
    return ajax.success_response(None, '禁用成功!')


@gma_login_required
def new_fast_reply(request):
    """ 添加动态表情 """
    context = helpers.build_context(request)
    context['new_fast_reply_id'] = reply_mgr.get_max_fast_reply_id() + 1
    return render(request, 'gma/config/reply/config_new_fast_reply.html', context)


@gma_login_required
def edit_fast_reply(request, fast_reply_id):
    """ 编辑动态表情 """
    context = helpers.build_context(request)
    context['fast_reply'] = reply_mgr.get_fast_reply(fast_reply_id)
    return render(request, 'gma/config/reply/config_edit_fast_reply.html', context)


@gma_login_required
def api_new_fast_reply(request):
    """ 添加新的动态表情 """
    id = request.POST['id'].strip()
    message = request.POST['message'].strip()
    orderno = int(request.POST['orderno'].strip())
    Log.info(f'Add new fast_reply: message={message}, orderno={orderno}')

    if not message:
        return ajax.error_response('回复不可以为空!')

    ok = reply_mgr.new_fast_reply(id, message, orderno)
    if not ok:
        return ajax.error_response('添加失败!')

    return ajax.success_response()


@gma_login_required
def api_edit_fast_reply(request):
    """ 编辑动态表情 """
    fast_reply_id = int(request.POST['fast_reply_id'].strip())
    fast_reply_message = request.POST['fast_reply_message'].strip()
    fast_reply_orderno = int(request.POST['fast_reply_orderno'].strip())

    if not fast_reply_message:
        return ajax.error_response('回复不可以为空!')

    ok = reply_mgr.edit_fast_reply(fast_reply_id, fast_reply_message, fast_reply_orderno)
    if not ok:
        return ajax.error_response('修改失败!')
    return ajax.success_response(None, '修改成功!')


if __name__ == '__main__':
    print('ok')
